﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using mal.balades.Business.DataModel;
using mal.Web.Routing;
using mal.balades.Business.Log;
using System.IO;

namespace mal.balades.Web
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            FileInfo log4NetConfigInfo = new FileInfo(Server.MapPath("Log4Net.config"));
            log4net.Config.XmlConfigurator.Configure(log4NetConfigInfo);

            MalLogManager.WebLogger.Info("Application Start");
            BaladesDataContext.ConnectionStringName = "MalBaladesConnectionString";
            RouteConfigurationManager.Initialize();
        }

        void Application_End(object sender, EventArgs e)
        {
            MalLogManager.WebLogger.Info("Application End");

        }

        void Application_Error(object sender, EventArgs e)
        {
            Exception exp = Server.GetLastError();

            if (exp != null)
            {
                if (exp is HttpException)
                {
                    HttpException httpexp = (HttpException)exp;

                    if (httpexp.GetHttpCode() == 404)
                    {
                        MalLogManager.ErrorLogger.WarnFormat("{0} : {1}"
                           , httpexp.Message, HttpContext.Current.Request.Url );
                    }
                    else
                    {
                        MalLogManager.ErrorLogger.Error(string.Format("Code :{0}, {1}"
                            , httpexp.ErrorCode, httpexp.Message), exp);
                    }

                }
                else
                {
                    MalLogManager.ErrorLogger.Error(string.Format("{0}", exp.Message), exp);
                }
            }
            else
            {
                MalLogManager.ErrorLogger.Error(exp.Message,exp);
            }
            Response.Redirect("/ErrorPage.aspx");
        }


        protected void Application_PostAcquireRequestState(object sender, EventArgs e)
        {
            if (HttpContext.Current != null && HttpContext.Current.Session != null)
                log4net.ThreadContext.Properties["SessionId"] = HttpContext.Current.Session.SessionID;
        } 

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            //log4net.ThreadContext.Properties["SessionId"] = Session.SessionID; 
        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.

        }

    }
}
